﻿using System;
using System.Collections.Generic;
using SystemModule;

namespace M2Server
{
    /// <summary>
    /// 地图事件管理
    /// </summary>
    public class TEventManager
    {
        /// <summary>
        /// 事件列表
        /// </summary>
        private IList<TEvent> m_EventList = null;
        /// <summary>
        /// 关闭事件列表
        /// </summary>
        private IList<TEvent> m_ClosedEventList = null;

        public void Run()
        {
            TEvent __Event = null;
            try
            {
                for (var i = m_EventList.Count - 1; i >= 0; i--)
                {
                    if (m_EventList.Count <= 0)
                    {
                        break;
                    }
                    __Event = m_EventList[i];
                    if (__Event != null)
                    {
                        if (__Event.m_boActive && ((HUtil32.GetTickCount() - __Event.m_dwRunStart) > __Event.m_dwRunTick))
                        {
                            __Event.m_dwRunStart = HUtil32.GetTickCount();
                            __Event.Run();
                            if (__Event.m_boClosed)
                            {
                                m_ClosedEventList.Add(__Event);
                                m_EventList.RemoveAt(i);
                            }
                        }
                    }
                }

                for (int i = m_ClosedEventList.Count - 1; i >= 0; i--)
                {
                    if (m_ClosedEventList.Count <= 0)
                    {
                        break;
                    }
                    __Event = m_ClosedEventList[i];
                    if (__Event != null)
                    {
                        if ((HUtil32.GetTickCount() - __Event.m_dwCloseTick) > 300000) // 5 * 60 * 1000
                        {
                            m_ClosedEventList.RemoveAt(i);
                            if (__Event != null)
                            {
                                __Event = null;
                            }
                        }
                    }
                }
            }
            catch
            {
                M2Share.MainOutMessage("[异常] TEventManager.Run");
            }
        }

        public int GetRangeEvent(Envirnoment Envir, TBaseObject OwnBaseObject, int nX, int nY, int nRange, int nType)
        {
            int result = 0;
            TEvent __Event;
            if (m_EventList.Count > 0)
            {
                for (var i = 0; i < m_EventList.Count; i++)
                {
                    __Event = ((m_EventList[i]) as TEvent);
                    if ((__Event.m_OwnBaseObject == OwnBaseObject) && (Math.Abs(__Event.m_nX - nX) <= nRange) && (Math.Abs(__Event.m_nY - nY) <= nRange) && (__Event.m_nEventType == nType))
                    {
                        result++;
                    }
                }
            }
            return result;
        }

        public TEvent GetEvent(Envirnoment Envir, int nX, int nY, int nType)
        {
            TEvent result = null;
            TEvent __Event = null;
            if (m_EventList.Count > 0)
            {
                for (var i = 0; i < m_EventList.Count; i++)
                {
                    __Event = m_EventList[i];
                    if ((__Event.m_Envir == Envir) && (__Event.m_nX == nX) && (__Event.m_nY == nY) && (__Event.m_nEventType == nType))
                    {
                        result = __Event;
                        break;
                    }
                }
            }
            return result;
        }

        public TEvent FindEvent(Envirnoment Envir, TEvent __Event)
        {
            TEvent result = null;
            if (m_EventList.Count > 0)
            {
                for (var i = 0; i < m_EventList.Count; i++)
                {
                    if ((m_EventList[i].m_Envir == Envir) && (m_EventList[i] == __Event))
                    {
                        result = m_EventList[i];
                        break;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 添加事件
        /// </summary>
        /// <param name="__Event"></param>
        public void AddEvent(TEvent __Event)
        {
            m_EventList.Add(__Event);
        }

        public TEventManager()
        {
            m_EventList = new List<TEvent>();
            m_ClosedEventList = new List<TEvent>();
        }

        ~TEventManager()
        {
            if (m_EventList != null)
            {
                if (m_EventList.Count > 0)
                {
                    for (var i = 0; i < m_EventList.Count; i++)
                    {
                        Dispose(m_EventList[i]);
                    }
                }
                Dispose(m_EventList);
            }

            if (m_EventList != null)
            {
                if (m_ClosedEventList.Count > 0)
                {
                    for (var i = 0; i < m_ClosedEventList.Count; i++)
                    {
                        Dispose(m_ClosedEventList[i]);
                        //  ((m_ClosedEventList[i]) as TEvent).Free;
                    }
                }
                Dispose(m_ClosedEventList);
            }
        }

        /// <summary>
        /// 释放指定对象资源
        /// </summary>
        /// <param name="obj"></param>
        private void Dispose(object obj)
        {
            obj = null;
        }
    }
}